/**
 * area edit
 */

layui.use(['iconPickerFa', 'form', 'tableSelect'], function () {
    let form = layui.form,
        layer = layui.layer,
        iconPickerFa = layui.iconPickerFa,
        tableSelect = layui.tableSelect,
        $ = layui.$;

    let u = localStorage.getItem("a@m");
    let loginU = {};
    if (u == undefined) {
        // layer.msg('请登录', {icon: 1, time: 500}, function () {
        //     window.location = 'page/login.html';
        // });
    } else {
        loginU = decryptLocalStorage(u);
    }
    let _data = localStorage.getItem("areaData");
    let viewData = undefined;
    if (_data != undefined) {
        viewData = decryptLocalStorage(_data, loginU.token);
        $("#parentId").val(viewData.parentId);
        $("#parentName").val(viewData.parentName);
        $("#title").val(viewData.title);
        $("#href").val(viewData.href);
        $("#icon").val(viewData.icon);
        $("#target").val(viewData.target);
        $("#sort").val(viewData.sort);

    }
    //监听提交
    form.on('submit(saveBtn)', function (data) {
        var params = data.field;
        params.token = loginU.token;
        //添加
        if (viewData == undefined) {
            request.add('menu', JSON.stringify(params), function (result) {
                if (result.code == 0) {
                    layer.msg('保存成功', {
                        icon: 1,
                        time: 500
                    }, function () {
                        // 关闭弹出层
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    });

                } else {
                    layer.msg('保存失败', {
                        icon: 2,
                        time: 2000
                    });
                }
            });
        }
        //修改
        else {
            params.menuId = viewData.menuId;
            request.update('menu', JSON.stringify(params), function (result) {
                if (result.code == 0) {
                    layer.msg('保存成功', {
                        icon: 1,
                        time: 500
                    }, function () {
                        // 关闭弹出层
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    });

                } else {
                    layer.msg('保存失败', {
                        icon: 2,
                        time: 2000
                    });
                }
            });
        }

        return false;
    });


    let tableUrl = "/admin/v1/menu/page?token=" + loginU.token;
    tableSelect.render({
        elem: '#parentName',
        checkedKey: 'id',
        table: {
            url: tableUrl,loginUserType:loginU.userType,
            cols: [[
                {type: 'radio'},
                {field: 'menuId', title: 'ID'},
                {field: 'title', title: '菜单'}
            ]]
        },
        done: function (elem, data) {
            if (data.data.length > 0) {
                if (viewData != undefined && data.data[0].menuId == viewData.menuId) {
                    layer.msg('不能选择自己为上级菜单', {
                        icon: 2,
                        time: 2000
                    });
                    return;
                }
                $("#parentId").val(data.data[0].menuId);
                $("#parentName").val(data.data[0].title);
            }
        },
        clear: function () {
            $("#parentId").val("");
            $("#parentName").val("");
        }
    })

    iconPickerFa.render({
        // 选择器，推荐使用input
        elem: '#icon',
        // fa 图标接口
        url: "/lib/font-awesome-4.7.0/less/variables.less",
        // 是否开启搜索：true/false，默认true
        search: true,
        // 是否开启分页：true/false，默认true
        page: true,
        // 每页显示数量，默认12
        limit: 24,
        // 点击回调
        click: function (data) {
            // if(data.icon!=undefined)
            // {
            //     let icon="fa "+data.icon;
            //     $("#icon").val(icon);
            // }
        },
        // 渲染成功后的回调
        success: function (d) {
            // console.log("success"+JSON.stringify(d));
        }
    });
});
